package org.jivesoftware.smackx.filetransfer;

import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.packet.StreamInitiation;

public class FileTransferManager
{
  private Connection connection;
  private final FileTransferNegotiator fileTransferNegotiator;
  private List<FileTransferListener> listeners;

  public FileTransferManager(Connection paramConnection)
  {
    this.connection = paramConnection;
    this.fileTransferNegotiator = FileTransferNegotiator.getInstanceFor(paramConnection);
  }

  private void initListeners()
  {
    this.listeners = new ArrayList();
    Connection localConnection = this.connection;
    1 local1 = new PacketListener(this)
    {
      public void processPacket()
      {
        this.this$0.fireNewRequest((StreamInitiation)paramPacket);
      }
    };
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketTypeFilter(StreamInitiation.class);
    arrayOfPacketFilter[1] = new IQTypeFilter(IQ.Type.SET);
    localConnection.addPacketListener(local1, new AndFilter(arrayOfPacketFilter));
  }

  public void addFileTransferListener(FileTransferListener paramFileTransferListener)
  {
    if (this.listeners == null)
      initListeners();
    synchronized (this.listeners)
    {
      this.listeners.add(paramFileTransferListener);
      return;
    }
  }

  protected IncomingFileTransfer createIncomingFileTransfer(FileTransferRequest paramFileTransferRequest)
  {
    if (paramFileTransferRequest == null)
      throw new NullPointerException("RecieveRequest cannot be null");
    IncomingFileTransfer localIncomingFileTransfer = new IncomingFileTransfer(paramFileTransferRequest, this.fileTransferNegotiator);
    localIncomingFileTransfer.setFileInfo(paramFileTransferRequest.getFileName(), paramFileTransferRequest.getFileSize());
    return localIncomingFileTransfer;
  }

  public OutgoingFileTransfer createOutgoingFileTransfer(String paramString)
  {
    return new OutgoingFileTransfer(this.connection.getUser(), paramString, this.fileTransferNegotiator.getNextStreamID(), this.fileTransferNegotiator);
  }

  // ERROR //
  protected void fireNewRequest(StreamInitiation paramStreamInitiation)
  {
    // Byte code:
    //   0: aconst_null
    //   1: checkcast 123	[Lorg/jivesoftware/smackx/filetransfer/FileTransferListener;
    //   4: pop
    //   5: aload_0
    //   6: getfield 34	org/jivesoftware/smackx/filetransfer/FileTransferManager:listeners	Ljava/util/List;
    //   9: astore_3
    //   10: aload_3
    //   11: monitorenter
    //   12: aload_0
    //   13: getfield 34	org/jivesoftware/smackx/filetransfer/FileTransferManager:listeners	Ljava/util/List;
    //   16: invokeinterface 127 1 0
    //   21: anewarray 129	org/jivesoftware/smackx/filetransfer/FileTransferListener
    //   24: astore 5
    //   26: aload_0
    //   27: getfield 34	org/jivesoftware/smackx/filetransfer/FileTransferManager:listeners	Ljava/util/List;
    //   30: aload 5
    //   32: invokeinterface 133 2 0
    //   37: pop
    //   38: aload_3
    //   39: monitorexit
    //   40: new 94	org/jivesoftware/smackx/filetransfer/FileTransferRequest
    //   43: dup
    //   44: aload_0
    //   45: aload_1
    //   46: invokespecial 136	org/jivesoftware/smackx/filetransfer/FileTransferRequest:<init>	(Lorg/jivesoftware/smackx/filetransfer/FileTransferManager;Lorg/jivesoftware/smackx/packet/StreamInitiation;)V
    //   49: astore 7
    //   51: iconst_0
    //   52: istore 8
    //   54: iload 8
    //   56: aload 5
    //   58: arraylength
    //   59: if_icmplt +11 -> 70
    //   62: return
    //   63: astore 4
    //   65: aload_3
    //   66: monitorexit
    //   67: aload 4
    //   69: athrow
    //   70: aload 5
    //   72: iload 8
    //   74: aaload
    //   75: aload 7
    //   77: invokeinterface 140 2 0
    //   82: iinc 8 1
    //   85: goto -31 -> 54
    //
    // Exception table:
    //   from	to	target	type
    //   12	40	63	finally
    //   65	67	63	finally
  }

  protected void rejectIncomingFileTransfer(FileTransferRequest paramFileTransferRequest)
  {
    StreamInitiation localStreamInitiation = paramFileTransferRequest.getStreamInitiation();
    IQ localIQ = FileTransferNegotiator.createIQ(localStreamInitiation.getPacketID(), localStreamInitiation.getFrom(), localStreamInitiation.getTo(), IQ.Type.ERROR);
    localIQ.setError(new XMPPError(XMPPError.Condition.forbidden));
    this.connection.sendPacket(localIQ);
  }

  public void removeFileTransferListener(FileTransferListener paramFileTransferListener)
  {
    if (this.listeners == null)
      return;
    synchronized (this.listeners)
    {
      this.listeners.remove(paramFileTransferListener);
    }
  }
}